package backTracing;

import java.util.ArrayList;
import java.util.List;

public class 电话字母 {
    static StringBuilder sb=new StringBuilder();
    static List<String> res=new ArrayList<>();

    public static void main(String[] args) {
        List<String> strings = letterCombinations("23");
        for (String string : strings) {
            System.out.println(string);
        }
    }
    public static List<String> letterCombinations(String digits) {
        if(digits.length()==0)
            return res;
        String[] strs={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        backtracking(digits,strs,0);
        return res;
    }

    public static void backtracking(String digits,String[] strs,int start){
        //终止条件
        if(digits.length()==start){
            //sb.toString就表示一个新String了，而不是sb本身
            res.add(sb.toString());
            return;
        }
        //回溯
//        '2'-'0'相当于50-48，即ASCII码相减获得他们的数字之差
        String str=strs[digits.charAt(start)-'2'];
//        for(int i=start;i<str.length();i++){
        for(int i=0;i<str.length();i++){
            sb.append(str.charAt(i));
            backtracking(digits,strs,start+1);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}
